import json

from re_common.baselibrary.utils.core.mdeprecated import try_except2_async, try_except2

from apps.crawler_platform.core_callback import *
from apps.crawler_platform.core_callback.err_callback import all_client_back_callback
from apps.crawler_platform.core_platform.base_etl import BaseETL
from apps.crawler_platform.core_platform.core_etl import CoreRemoteEtlControllerMixin
from apps.crawler_platform.core_platform.core_g import SQLTable
from apps.crawler_platform.core_platform.core_sql import CoreSqlMixin
from apps.crawler_platform.core_platform.g_model import EtlDealModel, \
    JournalArticleETLMode, InputPlatformModel, \
    JournalEtlArticleModel
from apps.crawler_platform.qk_platform.base_qk_step import BaseQKItems

# 不执行逻辑，目的是为了不让 from apps.crawler_platform.core_callback import *
# 被编辑器认为没有使用，快捷清除时就不会被清除

STATICA_HACK = False
# globals()['kcah_acitats'[::-1].upper()] = False
if STATICA_HACK:  # pragma: no cover
    null()


class EtlItems(BaseQKItems):

    def __init__(self):
        super(EtlItems, self).__init__()
        self.task_setting = None
        self.rawid: list = []
        self.field: dict = {}
        self.down_model_x = None
        self.deal_model: EtlDealModel = EtlDealModel()

    def get_pkm(self):
        """
        获取主键
        :return:
        """
        return {"task_name": self.task_name,
                "task_tag": self.task_tag,
                "rawid": self.rawid,
                }


class EtlRemoteRef(BaseETL, CoreRemoteEtlControllerMixin):

    def __init__(self, request, inputs):
        super().__init__()
        self.err_tag = "etl_remote_ref"
        self.pm = EtlItems()

        self.input = inputs
        self.pm.task_name = inputs.data.task_name
        self.pm.task_tag = inputs.data.task_tag
        self.pm.rawid = inputs.data.rawid

        self.pm.redis = request.app.state.redis
        self.request = request
        self.step_info = ""
        self.sql_model: JournalEtlArticleModel

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def get_sql(self):
        self.pm.task_setting = self.redis_model.dict()["parse_dict"]["1_1"]["task_set"]
        # table_name = "re_parse_ref"
        table_name = self.pm.task_setting["table_name"]
        rows = await CoreSqlMixin.get_etl_remote_article(
            table_name,
            self.pm.task_setting["table_index"],
            self.pm.task_name,
            self.pm.task_tag,
            self.pm.rawid)
        if rows:
            self.pm.sql_model = []
            if len(rows) == 1:
                row = rows[0]
                if row.get(self.pm.task_setting["final_stat"], "") in [9]:
                    # self.pm.sql_model = {row["lngid"]:JournalEtlArticleModel.parse_obj(row)}
                    self.pm.sql_model.append(JournalEtlArticleModel.parse_obj(row))
                    return True, rows
            for row in rows:
                # self.pm.sql_model[row["lngid"]] = JournalEtlArticleModel.parse_obj(row)
                self.pm.sql_model.append(JournalEtlArticleModel.parse_obj(row))
            return True, rows
        else:
            return False, {
                "err_msg": "article remote etl sql查询不存在;{},{}".format(self.pm.task_tag, self.pm.rawid)}

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def save_sql(self, code, err_info):
        """
        与关系型sql的处理 包括存储下一级的sql以及本级sql的状态和字段更新
        """
        # table_name = "re_parse_ref"
        table_name = self.pm.task_setting["table_name"]
        table_index = self.pm.task_setting["table_index"]
        stname = self.pm.task_setting["final_stat"]
        if code == "8":
            suc_rawid = self.pm.rawid
            if isinstance(err_info, dict):
                for k, v in err_info.items():
                    suc_rawid.remove(k) 
                    await CoreSqlMixin.update_etl_remote_article(table_name, table_index, stname, self.pm.task_name,
                                                                 self.pm.task_tag, [k], "7", v)
            if len(suc_rawid) > 0:
                await CoreSqlMixin.update_etl_remote_article(table_name, table_index, stname, self.pm.task_name,
                                                             self.pm.task_tag, suc_rawid, code, "")
        else:
            await CoreSqlMixin.update_etl_remote_article(table_name, table_index, stname, self.pm.task_name,
                                                         self.pm.task_tag, self.pm.rawid, code,
                                                         err_info)
        return True, ""

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def do_etl_remote(self):
        tmp_dict = self.input.dict()["data"]
        tmp_dict["sub_db_id"] = self.pm.sql_model[0].dict()["sub_db_id"]
        tmp_dict["table_latest"] = json.loads(self.pm.task_setting["table_latest"])
        etl_input = InputPlatformModel(data=JournalArticleETLMode.parse_obj(tmp_dict))
        bools, dicts = await super().do_etl_remote_ref(etl_input)
        return bools, dicts

    @try_except2_async(callback=all_client_back_callback, is_print=True)
    async def run_task(self):
        bools, err_info = await self.task_step()
        return bools, err_info

    async def my_task(self, table=""):
        """
        接入调用的task
        ZCN:函数内部有逻辑,因此返回的上层的布尔值
        :return:
        """
        bools, err_info = await super().my_task(SQLTable.journal_article)
        return bools, err_info

    def deal_html(self, *args, **kwargs):
        """
        处理html
        :return:
        """
        super().deal_html(*args, **kwargs)

    @try_except2()
    def verify_html(self):
        """
        验证html
        """
        return True, ""

    @try_except2()
    def verify_para(self):
        """
        验证解析出来的数据
        """
        return True, {}
